//
//  ProjectData.m
//  OutesIOS
//
//  Created by norman on 2018/11/23.
//  Copyright © 2018 norman. All rights reserved.
//

#import "ProjectData.h"

@implementation ProjectData

-(id)initWithCoder:(NSCoder *)aDecoder{
    if (self = [super init]) {
        [self decode:aDecoder];
    }
    return self;
}

/**
 *  将对象写入文件的时候调用
 *  怎样存储
 *
 *  @param aCoder aCoder
 */
-(void)encodeWithCoder:(NSCoder *)aCoder{
    [self encode:aCoder];
}

-(NSDictionary *)objectClassInArray {
    return @{@"rooms":[RoomData class], @"main_equip_systems":[MainEquipSystemData class], @"qrjhq_systems":[QrjhqSystemData class], @"kqnrsj_systems":[KqnrsjSystemData class], @"water_cleaner_equips":[WaterCleanerEquipData class]};
}

- (NSDictionary*)selectFCUWithColdLoad:(double)coldLoad {
    if (self.fcuEquips == nil || self.fcuEquips.count == 0) {
        return nil;
    }
    NSDictionary *last = [self.fcuEquips objectAtIndex:self.fcuEquips.count - 1];
    int count = 1;
    if ([[last objectForKey:@"TotalCapacity"] doubleValue] < coldLoad) {
        count = ceil(coldLoad/[[last objectForKey:@"TotalCapacity"] doubleValue]);
        coldLoad = coldLoad/count;
    }
    for (NSDictionary *equip in self.fcuEquips) {
        if ([[equip objectForKey:@"TotalCapacity"] doubleValue] >= coldLoad) {
            [equip setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
            return equip;
        }
    }
    return nil;
}

- (NSDictionary*)selectSwjWithColdLoad:(double)coldLoad andHeatLoad:(double)heatLoad {
    [[DBManager manager]open:DB_WaterEquip];
    NSArray *allRecord = [[DBManager manager]queryArrayData:[NSString stringWithFormat:@"select * from WaterEquip where HeatValue >= %f and ColdValue >= %f", heatLoad, coldLoad]];
    NSDictionary *last = [[DBManager manager]queryOneRowData:@"select * from WaterEquip where Id = 'DB2B6E253EAC2FA9C2BA623749C48D17'"];
    [[DBManager manager]close];
    if (allRecord != nil && allRecord.count > 0) {
        return [allRecord objectAtIndex:0];
    } else {
        return last;
    }
}

@end
